package com.example.testproject.activity

import android.annotation.SuppressLint
import android.content.Context
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.entity.AbstractExpandableItem
import com.chad.library.adapter.base.entity.MultiItemEntity
import com.example.testproject.R
import com.example.testproject.adapter.SelectAdapter
import kotlinx.android.synthetic.main.recylcer_view.*

/**
 *
 * @ClassName: SelectActivity
 * @Author: android 超级兵
 * @CreateDate: 10/19/21$ 3:01 PM$
 * TODO 单选多选
 */
class SelectActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.recylcer_view)

        val tempList = initData().filter {
            it is SelectParentBean
        }.map {
            it as SelectParentBean
        }.map { parent ->
            parent
        }.toMutableList()

        val selectAdapter = SelectAdapter(tempList)

        rootRecyclerView.layoutManager = LinearLayoutManager(this)

        rootRecyclerView.adapter = selectAdapter

        selectAdapter.click = object : SelectAdapter.OnSelectAdapterClickListener {
            override fun onParentClick(position: Int, item: SelectParentBean) {
                selectAdapter.data.forEachIndexed { index, it ->
                    if (it is SelectParentBean) {
                        selectAdapter.recyclerList[index].visibility =
                            if (it.isVisibility) View.VISIBLE else View.GONE
                    }
                }

                selectAdapter.notifyDataSetChanged()
            }
        }

        val button = Button(this)
        button.text = "点我查看选中"
        selectAdapter.setFooterView(button)
        button.setOnClickListener {
            button.text = "\n"
            selectAdapter.data.filter {
                it is SelectParentBean
            }.map {
                it as SelectParentBean
            }.flatMap {
                it.child
            }.filter {
                it.isChildSelect
            }.map {
                button.append("${it.title}\n")
            }
        }
    }

    @SuppressLint("ShowToast")
    infix fun Context.toast(any: Any) {
        Toast.makeText(this, any.toString(), Toast.LENGTH_SHORT).show()
    }

    private fun initData() = listOf<MultiItemEntity>(
        SelectParentBean("性别",
            true,
            listOf(
                SelectParentBean.SelectChildBean("男"),
                SelectParentBean.SelectChildBean("女"),
            )),

        SelectParentBean("口味",
            false,
            listOf(
                SelectParentBean.SelectChildBean("清淡"),
                SelectParentBean.SelectChildBean("微辣"),
                SelectParentBean.SelectChildBean("巨辣"),
            )),

        SelectParentBean("地址",
            true,
            listOf(
                SelectParentBean.SelectChildBean("山西"),
                SelectParentBean.SelectChildBean("山东"),
                SelectParentBean.SelectChildBean("上海"),
                SelectParentBean.SelectChildBean("北京"),
                SelectParentBean.SelectChildBean("河南"),
            )),

        SelectParentBean("专业",
            false,
            listOf(
                SelectParentBean.SelectChildBean("android"),
                SelectParentBean.SelectChildBean("flutter"),
                SelectParentBean.SelectChildBean("ios"),
            )),
    )
}


data class SelectParentBean(
    val title: String,    // 标题
    val isSingleChoice: Boolean,// true单选
    val child: List<SelectChildBean>, // 子类
    var layoutId: Int = SelectAdapter.SELECT_PARENT,
    var isVisibility: Boolean = false, // 默认不显示
) : MultiItemEntity, AbstractExpandableItem<SelectParentBean.SelectChildBean>() {

    data class SelectChildBean(
        val title: String, // 名字
        var isChildSelect: Boolean = false,
    ) : MultiItemEntity {
        override fun getItemType(): Int = SelectAdapter.SELECT_CHILD
    }

    override fun getItemType(): Int = layoutId

    override fun getLevel(): Int = layoutId
}